// pages/limit_goods/limit_goods.js
var common = require("../../../common/common.js").common;

var shoppingCar = require("../../../common/shoppingCar.js");

var util = require("../../../utils/util.js");

var leader = require("../../../common/leader.js").leader;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        toastHide: true,
        myleaderinfo: {
            leader_id: -1,
            leader_id_true: -1
        },
        goods: [],
        chooseGoods: [],
        classify: [],
        alert1: 0,
        selectClass: 0,
        tabBar: [],
        tabBar1: [ {
            iconName: "icon-shouye1",
            tip: "首页",
            current: 1,
            num: 0,
            url: "/pages/index/index",
            openType: "switchTab"
        }, {
            iconName: "icon-fenlei1",
            tip: "分类",
            current: 0,
            num: 0,
            url: "/pages/classfit/classfit",
            openType: "switchTab"
        }, {
            iconName: "icon-gouwuche2",
            tip: "购物车",
            current: 0,
            num: 0,
            url: "/pages/shopcart/shopcart"
        }, {
            iconName: "icon-dingdan2",
            tip: "订单",
            current: 0,
            num: 0,
            url: "/pages/order/order",
            openType: "switchTab"
        }, {
            iconName: "icon-wode1",
            tip: "我的",
            current: 0,
            num: 0,
            url: "/pages/personal/personal",
            openType: "switchTab"
        } ],
        tmplIds: [],
        //模板消息tmplIds
        subscribeMessage: true,
        minimum_num: 1,
        step_num: 1,
        joinCate: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        //获取新用户专享商品
        this.getBase();
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        common.getBottomIcon().then(() => {
            common.getCart();
        });
        // this.getClass();
                this.getClassify();
        this.getTmplIds();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let that = this;
        leader.getMyLeaderInfo(wx.getStorageSync("user").openid);
        common.getCart();
        let newShoppingCart = shoppingCar.addPro(that.data.goods);
        var newGoods = that.changeProInfo(that.data.goods, newShoppingCart);
        that.setData({
            chooseGoods: newGoods,
            cart_count: newShoppingCart.cart_count
        });
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                // wx.setNavigationBarColor({
                //   frontColor: "#ffffff",
                //   backgroundColor: res.extra.color_main
                // })
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    sku_num_display: res.extra.sku_num_display
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    },
    //获取限量商品
    getLimitProduct: function(openid) {
        let that = this;
        var openid = openid ? openid : wx.getStorageSync("user").openid;
        common.request({
            endpoint: "product/pro_limit",
            method: "post",
            data: {
                open_id: openid
            },
            success: res => {
                if (!!res.extra.length > 0) {
                    that.changeProInfo(res.extra);
                    that.setData({
                        goods: res.extra
                    });
                }
                var chooseGoods = [];
                if (that.data.selectClass == -1) {
                    chooseGoods = that.data.goods;
                } else {
                    that.data.goods.map(x => {
                        if (x.category_id == that.data.selectClass) {
                            chooseGoods.push(x);
                        }
                    });
                }
                that.setData({
                    chooseGoods: chooseGoods
                });
            }
        });
    },
    /**
 * 商品信息修改
*/
    //首页商品模块，先获取分类，再取商品
    getClass: function() {
        this.setData({
            index_choose: this.data.index_choose,
            page: 1,
            list: [],
            showLoading: 1,
            goods: []
        });
        common.request({
            endpoint: "product/classification",
            method: "post",
            data: {
                page_type: 0
            },
            success: res => {
                if (!res.extra.data[0]) {
                    var chooseClass = -1;
                } else {
                    var chooseClass = res.extra.data[0].id;
                    this.setData({
                        classification: res.extra.data
                    });
                }
                this.setData({
                    chooseClass: chooseClass
                });
                this.getIndexNewGoods(chooseClass, 1);
            }
        });
    },
    //*******************************************************
    //
    // 整理合并首页所有请求 hgj 2019.11.03 
    // 下面开始是商品添加购物车
    // 怎么这么多行？肯定要合并
    // 
    //
    //*******************************************************
    join_cart: function(e) {
        let that = this;
        var id = e.currentTarget.dataset.id;
        var single = e.currentTarget.dataset.single;
        that.setData({
            joinCate: 1
        });
        common.request({
            endpoint: "product/new_customer_judge",
            method: "post",
            data: {
                open_id: wx.getStorageSync("user").openid,
                id: id
            },
            success: res => {
                if (res.code == 0) {
                    that.join_cart_current(e);
                } else {
                    that.toastControl("此商品只能新用户购买哟");
                }
            },
            fail() {
                that.toastControl("此商品只能新用户购买哟");
            }
        });
    },
    //加入购物车 此处商品详情接口需要跳转，并且增加加入中的提示
    join_cart_current(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var single = e.currentTarget.dataset.single;
        common.request({
            // endpoint: 'product/product_info',
            endpoint: "product/shopping-push",
            method: "post",
            data: {
                id: id
            },
            success: res => {
                let details = res.extra;
                details.sku_attribute = details.sku_attribute.map(item => {
                    item.attribute_value = this.selectSingleSku(item.attribute_value);
                    return item;
                });
                let sku_attribute = details.sku_attribute;
                let sku = sku_attribute.reduce((s, v) => {
                    s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
                    return s;
                }, "");
                let sku_info = this.getProductBySku(details.sku, sku);
                let cart_count = wx.getStorageSync("cart_count");
                if (single == 1) {
                    that.setData({
                        minimum_num: e.currentTarget.dataset.minimumnum,
                        step_num: e.currentTarget.dataset.stepnum
                    });
                    that.join_cart_current_ch(details, e);
                    // if (that.data.subscribeMessage) {
                    //   wx.requestSubscribeMessage({
                    //     tmplIds: that.data.tmplIds,
                    //     success(res) {
                    //     },
                    //     fail(err) {
                    //       console.log(err);
                    //     }
                    //   })
                    // }
                                } else {
                    that.setData({
                        details: details,
                        sku: sku,
                        alert: 1,
                        product_id: id,
                        sku_info: sku_info,
                        proId: id,
                        buy_count: 1
                    });
                }
            }
        });
    },
    //单规格商品购买
    join_cart_current_ch: function(details, e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        let single = e.currentTarget.dataset.single;
        details.sku_attribute = details.sku_attribute.map(item => {
            item.attribute_value = this.selectSingleSku(item.attribute_value);
            return item;
        });
        let sku_attribute = details.sku_attribute;
        let sku = sku_attribute.reduce((s, v) => {
            s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
            return s;
        }, "");
        let sku_info = this.getProductBySku(details.sku, sku);
        // let cart_count = wx.getStorageSync("cart_count")
                if (sku_info.stock_number >= 1) {
            that.cartRequest(sku_info.item_id, "add", id, 1);
        } else {
            wx.showToast({
                title: "库存不足",
                icon: "none"
            });
        }
    },
    cartRequest(sku, type, id, single) {
        let that = this;
        let num = 0;
        if (single == 1) {
            var addC = that.data.joinCate == 1 ? parseInt(that.data.minimum_num) : parseInt(that.data.step_num);
            num = type == "add" ? addC : -addC;
        } else {
            num = that.data.buy_count;
        }
        let goods = that.data.goods;
        let i = goods.findIndex(item => item.id == id);
        if (type != "add") {
            if (goods[i].cart_num <= parseInt(that.data.minimum_num)) {
                num = -goods[i].cart_num;
            }
        }
        common.request({
            endpoint: "Order/cart/add",
            method: "post",
            data: {
                leader_id: that.data.myleaderinfo.leader_id != -1 ? that.data.myleaderinfo.leader_id : 0,
                sku_id: sku,
                business_id: 0,
                num: num,
                flag: 0,
                type: 0
            },
            success: res => {
                if (single == 1) {
                    if (type == "add") {
                        goods[i].cart_num += num;
                    } else {
                        goods[i].cart_num += num;
                    }
                } else {
                    goods[i].cart_num += that.data.buy_count;
                }
                if (goods[i].cart_num < 0) {
                    goods[i].cart_num = 0;
                }
                that.setData({
                    chooseGoods: goods,
                    alert: 0
                });
                if (that.data.subscribeMessage) {
                    wx.requestSubscribeMessage({
                        tmplIds: that.data.tmplIds,
                        success(res) {},
                        fail(err) {
                            console.log(err);
                        },
                        complete() {}
                    });
                }
            },
            fail: err => {
                wx.showModal({
                    title: "提示",
                    content: err.message
                });
            }
        });
    },
    //提示框
    toastControl(content, time) {
        var that = this;
        that.setData({
            toastContent: content,
            toastHide: false
        });
        //显示提示框
                setTimeout(() => {
            that.setData({
                toastHide: true
            });
        }, !time ? 3e3 : time);
        //定时隐藏提示框,默认3秒后隐藏
        },
    /** 
   * 添加 sku 属性
   */
    addSkuAttribute: function(e) {
        let key_id = parseInt(e.currentTarget.dataset.key_id);
        let value_id = parseInt(e.currentTarget.dataset.value_id);
        let a_key_index = parseInt(e.currentTarget.dataset.a_key_index);
        let a_value_index = parseInt(e.currentTarget.dataset.a_value_index);
        let sku = this.data.sku;
        sku = this.reBlanceSku(sku, key_id, value_id);
        let attribute = this.data.details.sku_attribute;
        attribute = this.selectSku(attribute, a_key_index, a_value_index);
        let sku_info = this.getProductBySku(this.data.details.sku, sku);
        this.setData({
            sku: sku,
            ["details.sku_attribute"]: attribute,
            sku_info: sku_info
        });
    },
    /** 
   * 重平衡 sku
   * key_id: integer
   */
    reBlanceSku: function(sku, key_id, value_id) {
        let sku_lib = sku != "" ? this.splitSku(sku) : [];
        sku_lib = this.addSku(sku_lib, {
            key_id: key_id,
            value_id: value_id
        });
        sku_lib = this.sortSku(sku_lib);
        return this.buildSku(sku_lib);
    },
    /** 
   * 重建 sku
   */
    buildSku: function(sku_lib) {
        return sku_lib.map(item => {
            return item.key_id + "," + item.value_id;
        }).join("|");
    },
    /** 
   * 对选择的 sku 属性进行排序
   */
    sortSku: function(sku_lib) {
        return sku_lib.sort((pre, next) => {
            if (pre.key_id > next.key_id) {
                return 1;
            } else if (pre.key_id < next.key_id) {
                return -1;
            } else {
                return 0;
            }
        });
    },
    /** 
   * 分割现有 sku 成对象数组
   */
    splitSku: function(sku) {
        return sku.split("|").map(sku_tmp => {
            let [key, value] = sku_tmp.split(",");
            const t = {};
            t.key_id = parseInt(key);
            t.value_id = parseInt(value);
            return t;
        });
    },
    /** 
   * 添加一个 sku
   */
    addSku: function(sku_lib, sku) {
        let t = sku_lib.filter(item => {
            if (item.key_id === sku.key_id) {
                return false;
            }
            return true;
        });
        t.push(sku);
        return t;
    },
    /** 
   * 选择 sku
   */
    selectSku: function(attribues, a_index, v_index = 0) {
        if (attribues[a_index]) {
            let values = attribues[a_index].attribute_value;
            values = values.map(item => {
                item.selected = false;
                return item;
            });
            values[v_index].selected = true;
            attribues[a_index].attribute_value = values;
        }
        return attribues;
    },
    /** 
   * 选择单个 sku 属性
   */
    selectSingleSku: function(attribute_value, v_index = 0) {
        attribute_value.map(item => {
            item.selected = false;
            return item;
        });
        attribute_value[v_index].selected = true;
        return attribute_value;
    },
    //加入成功后，更新购物车数量
    save_count: function(e) {
        var that = this;
        //判断是否是新人商品
                if (that.data.addShoppingCar) {
            //非新人商品
            this.setData({
                buy_count: parseInt(e.detail.value)
            });
        } else {
            if (that.data.newUserBuyLimit != 0) {
                if (parseInt(e.detail.value > that.data.newUserBuyLimit)) {
                    //判断数值大小有没有超过购买上限
                    //超过上限，设置为上限值
                    that.setData({
                        buy_count: that.data.newUserBuyLimit
                    });
                } else {
                    that.setData({
                        buy_count: parseInt(e.detail.value)
                    });
                }
            } else {
                that.setData({
                    buy_count: parseInt(e.detail.value)
                });
            }
        }
    },
    blur: function(e) {
        let count = e.detail.value;
        if (count <= 0) {
            count = 1;
        }
        this.setData({
            buy_count: parseInt(count)
        });
    },
    reduce_count: function() {
        let buy_count = this.data.buy_count;
        if (buy_count > 1) {
            buy_count--;
        }
        this.setData({
            buy_count: buy_count
        });
    },
    add_count: function(e) {
        let that = this;
        that.add_count_ch(e);
        if (that.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                }
            });
        }
    },
    add_count_ch: function(e) {
        let that = this;
        let restrictions = that.data.addShoppingCar ? e.currentTarget.dataset.restrictions : e.currentTarget.dataset.newlimit;
        let buy_count = this.data.buy_count;
        console.log(restrictions + "," + buy_count);
        if (restrictions == 0) {
            buy_count++;
        } else {
            if (buy_count >= restrictions) {
                wx.showModal({
                    title: "提示",
                    content: "本商品限购" + restrictions + "份"
                });
                buy_count = restrictions;
            } else {
                buy_count++;
            }
        }
        this.setData({
            buy_count: buy_count
        });
    },
    /**
   * 获取分类
   */
    getClassify: function() {
        let that = this;
        common.request({
            endpoint: "product/new_classify",
            method: "post",
            data: {
                param: 2
            },
            success: res => {
                that.setData({
                    classify: res.extra,
                    selectClass: res.extra[0].id
                });
                that.getLimitProduct();
            }
        });
    },
    /**
   * 根据 sku 获取物品
   */
    getProductBySku: function(sku_products, sku) {
        return sku_products[sku];
    },
    contactShop: function() {
        let that = this;
        var phone = that.data.phone;
        if (phone == "111" || phone == "") {} else {
            wx.makePhoneCall({
                phoneNumber: this.data.phone
            });
        }
    },
    buy: function(e) {
        let that = this;
        that.buy_ch(e);
        if (that.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                }
            });
        }
    },
    buy_ch: function(e) {
        var that = this;
        var alert = that.data.alert;
        var details = that.data.details;
        var sku_info = that.data.sku_info;
        var buy_count = that.data.buy_count;
        var id = that.data.goodsId;
        var sku_id = sku_info.item_id;
        let sku_item = sku_info.item_id + "," + buy_count + "," + sku_info.credit_sent + "," + details.only_express;
        var price = "";
        if (e.currentTarget.dataset.price) {
            price = e.currentTarget.dataset.price;
        }
        if (sku_info.stock_number >= buy_count) {
            wx.navigateTo({
                url: "/newPlace_order/pages/newPlace_order/newPlace_order?sku_item=" + sku_item + "&status=" + 0 + "&newuserprice=" + price
            });
            that.setData({
                alert: 0,
                addShoppingCar: true,
                newUserProductPrice: ""
            });
        } else {
            wx.showToast({
                title: "库存不足",
                icon: "none"
            });
            that.setData({
                alert: 0
            });
        }
    },
    join_goods: function() {
        let that = this;
        that.cartRequest(that.data.sku_info.item_id, "add", that.data.details.product_id, 2);
        // that.join_goods_ch();
        // if (that.data.subscribeMessage) {
        //   wx.requestSubscribeMessage({
        //     tmplIds: that.data.tmplIds,
        //     success(res) {
        //     },
        //     fail(err) {
        //       console.log(err);
        //     }
        //   })
        // }
        },
    join_goods_ch: function() {
        var that = this;
        var alert = that.data.alert;
        var cart_count = wx.getStorageSync("cart_count") || 0;
        var details = that.data.details;
        var sku_info = that.data.sku_info;
        var buy_count = that.data.buy_count;
        var id = that.data.product_id;
        cart_count = cart_count + buy_count;
        var cart = wx.getStorageSync("cart") || [];
        if (cart_count <= 99999) {
            if (sku_info.stock_number >= buy_count) {
                if (cart == "") {
                    let goods = {
                        id: sku_info.item_id,
                        product_id: id,
                        img: details.icon_img,
                        title: details.name,
                        varieties: sku_info.title,
                        price: sku_info.price,
                        o_price: sku_info.o_price,
                        count: buy_count,
                        stock_number: sku_info.stock_number,
                        select: 1,
                        proID: id,
                        status: 1,
                        credit_sent: sku_info.credit_sent || 0,
                        spec_fund: details.spec_fund,
                        is_vip: details.is_vip,
                        vip_price: sku_info.vip_price
                    };
                    wx.showToast({
                        title: "加入购物车成功",
                        icon: "none",
                        success: res => {
                            wx.setStorage({
                                key: "cart_count",
                                data: cart_count
                            });
                            this.setData({
                                cart_count: cart_count
                            });
                            common.changeIconNum(cart_count, 2);
                            var cart = wx.getStorageSync("cart") || that.data.cart;
                            if (!cart) {
                                cart = [];
                            }
                            cart.push(goods);
                            wx.setStorage({
                                key: "cart",
                                data: cart
                            });
                            setTimeout(() => {
                                let newShoppingCart = shoppingCar.getInfo();
                                var newPro = that.changeProInfo(that.data.goods, newShoppingCart);
                                that.setData({
                                    buy_count: 1,
                                    alert: 0,
                                    chooseGoods: newPro
                                });
                            }, 500);
                        }
                    });
                } else {
                    var total = 0;
                    var index = 0;
                    for (var i = 0; i < cart.length; i++) {
                        if (sku_info.item_id != cart[i]["id"]) {
                            total++;
                        } else {
                            index = i;
                            break;
                        }
                    }
                    if (total == cart.length) {
                        let goods = {
                            id: sku_info.item_id,
                            product_id: id,
                            img: details.icon_img,
                            title: details.name,
                            varieties: sku_info.title,
                            price: sku_info.price,
                            o_price: sku_info.o_price,
                            count: buy_count,
                            stock_number: sku_info.stock_number,
                            select: 1,
                            proID: id,
                            status: 1,
                            credit_sent: sku_info.credit_sent || 0,
                            spec_fund: details.spec_fund,
                            spec_fund: details.spec_fund,
                            is_vip: details.is_vip,
                            vip_price: sku_info.vip_price
                        };
                        wx.showToast({
                            title: "加入购物车成功",
                            icon: "none",
                            success: res => {
                                wx.setStorage({
                                    key: "cart_count",
                                    data: cart_count
                                });
                                this.setData({
                                    cart_count: cart_count
                                });
                                common.changeIconNum(cart_count, 2);
                                var cart = wx.getStorageSync("cart") || that.data.cart;
                                cart.push(goods);
                                wx.setStorage({
                                    key: "cart",
                                    data: cart
                                });
                                setTimeout(() => {
                                    let newShoppingCart = shoppingCar.getInfo();
                                    var newPro = that.changeProInfo(that.data.goods, newShoppingCart);
                                    that.setData({
                                        buy_count: 1,
                                        alert: 0,
                                        chooseGoods: newPro
                                    });
                                }, 500);
                            }
                        });
                    } else {
                        cart[index]["count"] = cart[index]["count"] + buy_count;
                        wx.showToast({
                            title: "加入购物车成功",
                            icon: "none",
                            success: res => {
                                wx.setStorage({
                                    key: "cart_count",
                                    data: cart_count
                                });
                                this.setData({
                                    cart_count: cart_count
                                });
                                common.changeIconNum(cart_count, 2);
                                wx.setStorage({
                                    key: "cart",
                                    data: cart
                                });
                                setTimeout(() => {
                                    let newShoppingCart = shoppingCar.getInfo();
                                    var newPro = that.changeProInfo(that.data.goods, newShoppingCart);
                                    that.setData({
                                        buy_count: 1,
                                        alert: 0,
                                        chooseGoods: newPro
                                    });
                                }, 500);
                            }
                        });
                    }
                }
            } else {
                wx.showToast({
                    title: "加入购物车失败",
                    icon: "none"
                });
            }
        } else {
            wx.showToast({
                title: "购物车最多加99999个商品！",
                icon: "none"
            });
        }
    },
    //提示框
    toastControl(content, time) {
        var that = this;
        that.setData({
            toastContent: content,
            toastHide: false
        });
        //显示提示框
                setTimeout(() => {
            that.setData({
                toastHide: true
            });
        }, !time ? 3e3 : time);
        //定时隐藏提示框,默认3秒后隐藏
        },
    /**
   * 
   * @param {详情} e 
   */
    gotoDetail(e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url
        });
    },
    //购物车减
    reduceCount(e) {
        let that = this;
        that.setData({
            joinCate: 2
        });
        if (e.currentTarget.dataset.single == 1) {
            var params = {
                id: e.currentTarget.dataset.id
            };
            that.setData({
                minimum_num: e.currentTarget.dataset.minimumnum,
                step_num: e.currentTarget.dataset.stepnum
            });
            common.request({
                // endpoint: 'product/product_info',
                endpoint: "product/shopping-push",
                method: "post",
                data: params,
                success: res => {
                    let details = res.extra;
                    this.setData({
                        commission_show: details.commission_show
                    });
                    details.sku_attribute = details.sku_attribute.map(item => {
                        item.attribute_value = this.selectSingleSku(item.attribute_value);
                        return item;
                    });
                    let sku_attribute = details.sku_attribute;
                    let sku = sku_attribute.reduce((s, v) => {
                        s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
                        return s;
                    }, "");
                    let sku_info = this.getProductBySku(details.sku, sku);
                    that.cartRequest(sku_info.item_id, "reduce", e.currentTarget.dataset.id, 1);
                }
            });
            common.changeIconNum(newShoppingCart.cart_count, 2);
        } else {
            wx.showToast({
                title: "该商品是多属性商品，请到购物车操作",
                icon: "none"
            });
        }
    },
    addCount(e) {
        let that = this;
        that.setData({
            joinCate: 2
        });
        if (e.currentTarget.dataset.item.buyCount >= e.currentTarget.dataset.item.all_stock_num) {
            wx.showModal({
                title: "提示",
                content: "库存不足,无法继续购买！",
                showCancel: false
            });
            return false;
        }
        that.addCount_ch(e);
        // if (that.data.subscribeMessage) {
        //   wx.requestSubscribeMessage({
        //     tmplIds: that.data.tmplIds,
        //     success(res) {
        //     },
        //     fail(err) {
        //       console.log(err);
        //     }
        //   })
        // }
        },
    //加
    addCount_ch(e) {
        let that = this;
        this.join_cart_current(e);
        // that.join_cart(e)
        },
    /**
   * 商品信息修改
   */
    changeProInfo(proInfo, cartInfoParamter) {
        var cartInfo = cartInfoParamter ? cartInfoParamter.statisticsArry : shoppingCar.getInfo().statisticsArry;
        if (proInfo.length > 0) {
            proInfo.forEach((item, index) => {
                if (cartInfo.length > 0) {
                    for (var i = 0; i < cartInfo.length; i++) {
                        if (item.id == cartInfo[i].product_id) {
                            item.buyCount = cartInfo[i].count;
                            break;
                        } else {
                            item.buyCount = 0;
                        }
                    }
                } else {
                    item.buyCount = 0;
                }
            });
        }
        return proInfo;
    },
    goToOtherFunction(e) {
        wx.navigateTo({
            url: "/localLifePackage/pages/pro_detail/pro_detail"
        });
    },
    //关闭蒙层
    close_alert: function(e) {
        this.setData({
            alert: 0
        });
    },
    choiceClassify(e) {
        let that = this;
        let id = e.currentTarget.dataset.id;
        that.setData({
            selectClass: id
        });
        var chooseGoods = [];
        if (id == -1) {
            //全部
            chooseGoods = that.data.goods;
        } else {
            that.data.goods.map(item => {
                if (item.category_id == id) {
                    chooseGoods.push(item);
                }
            });
        }
        that.setData({
            chooseGoods: chooseGoods
        });
    },
    //获取tmplIds
    getTmplIds: function() {
        let that = this;
        const version = wx.getSystemInfoSync().SDKVersion;
        //console.log(version);
                if (util.compareVersion(version, "2.8.2") < 0) {
            that.setData({
                tmplIds: [],
                subscribeMessage: false
            });
            return;
        }
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if ((v.type == "21" || v.type == "22" || v.type == "23") && v.status == "1") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    }
});